<?php

use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;

class Questions extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $this->table('questions')
            ->addColumn('type_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '题目类型ID'])
            ->addColumn('author_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '作者用户ID'])
            ->addColumn('style', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '0选择，1填空'])
            ->addColumn('image', 'string', ['limit' => 100, 'null' => true, 'comment' => '图片文件名'])
            ->addColumn('text', 'string', ['limit' => 200, 'comment' => '题干'])
            ->addColumn('options', 'string', ['limit' => 1000, 'comment' => '选项'])
            ->addColumn('answer', 'string', ['limit' => 100, 'comment' => '答案'])
            ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '题目状态，0未审核，1通过，2不通过'])
            ->addColumn('pass_count', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '做对次数'])
            ->addColumn('fail_count', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '做错次数'])
            ->addColumn('is_exported', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '是否已经导出'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['type_id'])
            ->addIndex(['author_id'])
            ->addIndex(['style'])
            ->addIndex(['status'])
            ->addIndex(['is_exported'])
            ->save();

        $this->table('types')
            ->addColumn('type_name', 'string', ['limit' => 50, 'comment' => '类型名称'])
            ->addColumn('parent_type_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '父类型ID'])
            ->addColumn('root_type_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '根类型ID'])
            ->addColumn('output_code', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '输出编号'])
            ->save();
    }

    public function down()
    {
        $this->table('questions')->drop();
        $this->table('types')->drop();
    }
}
